﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WinStudent
{
    public partial class FrmAddCourse : Form
    {
        public FrmAddCourse()
        {
            InitializeComponent();
        }

        private void FrmAddCourse_Load(object sender, EventArgs e)
        {

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string cno = txtCno.Text.Trim();
            string cname = txtCname.Text.Trim();
            if (string.IsNullOrEmpty(cno))
            {
                MessageBox.Show("课程号不能为空", "课程信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            try
            {
                int.Parse(txtCredit.Text.Trim());
            }
            catch (Exception)
            {
                MessageBox.Show("学分无效", "课程信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int credit = int.Parse(txtCredit.Text.Trim());
            try
            {
                int.Parse(txtSemester.Text.Trim());
            }
            catch (Exception)
            {
                MessageBox.Show("学期无效", "课程信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int semester = int.Parse(txtSemester.Text.Trim());

            {
                string sqlCno = "select count(Cno) from Course where Cno=@Cno";
                SqlParameter para = new SqlParameter("@Cno", cno);
                object oCno = SqlHelper.ExecuteScalar(sqlCno, para);
                if (oCno == null || oCno == DBNull.Value||(int)oCno==0)
                {
                    string sql = "insert into Course (Cno,Cname,Credit,Semester) values (@Cno,@Cname,@Credit,@Semester)";
                    SqlParameter[] paras =
                    {
                        new SqlParameter("@Cno",cno),
                        new SqlParameter("@Cname",cname),
                        new SqlParameter("@Credit",credit),
                        new SqlParameter("@Semester",semester)
                    };
                    int count = SqlHelper.ExecutNonQuery(sql, paras);

                    if (count > 0)
                    {
                        MessageBox.Show("课程信息添加成功", "课程信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("课程信息添加失败", "课程信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("课程号已存在", "课程信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
